package com.ce.source.analysis.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：线程优先级
 * @ClassName ：PriorityThread
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/3/5 10:59
 */
public class PriorityThread {

    private static AtomicInteger threadNo = new AtomicInteger(1);
    private static final Executor service = Executors.newScheduledThreadPool(5, new ThreadFactory() {
        public Thread newThread(Runnable r) {
            return new Thread(r, "threadNo_" + threadNo.getAndIncrement());
        }
    });

    public static void main(String[] args) {
        int count = 0;
        Level_1_Thread _level1 = new Level_1_Thread();
        _level1.setPriority(1);
        Level_4_Thread _level4 = new Level_4_Thread();
        _level4.setPriority(4);
        while (count < 100) {
//            service.execute(_level1);
//            service.execute(_level4);
            _level1.start();
            _level4.start();
            count++;
        }


    }

    static class Level_1_Thread extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread() + "执行优先级1");
        }
    }

    static class Level_4_Thread extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread() + "执行优先级4");
        }
    }
}
